package com.ddddddl.widget.scrollTab;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class FlingTabWidget extends LinearLayout implements OnFocusChangeListener
{

	private OnTabSelectionChanged selectionChangedListener;
	private int selectedTab;

	public FlingTabWidget(Context context)
	{
		this(context, null);
	}

	public FlingTabWidget(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		initTabWidget();
	}

	private void initTabWidget()
	{
		setOrientation(LinearLayout.HORIZONTAL);
		setFocusable(true);
		setOnFocusChangeListener(this);
	}

	void setTabSelectionListener(OnTabSelectionChanged listener)
	{
		selectionChangedListener = listener;
	}

	public View getChildTabViewAt(int index)
	{
		return getChildAt(index);
	}

	public void setCurrentTab(int index)
	{
		if (index < 0 || index >= getTabCount())
		{
			return;
		}

		getChildTabViewAt(selectedTab).setSelected(false);
		selectedTab = index;
		getChildTabViewAt(selectedTab).setSelected(true);
	}

	public void focusCurrentTab(int index)
	{
		final int oldTab = selectedTab;
		setCurrentTab(index);

		if (oldTab != index)
		{
			getChildAt(index).requestFocus();
		}
	}

	public int getTabCount()
	{
		int children = getChildCount();
		return children;
	}

	@Override
	public void setEnabled(boolean enabled)
	{
		super.setEnabled(enabled);
		int count = getTabCount();

		for (int i = 0; i < count; i++)
		{
			View child = getChildTabViewAt(i);
			child.setEnabled(enabled);
		}
	}

	@Override
	public void addView(View child)
	{
		if (child.getLayoutParams() == null)
		{
			final LinearLayout.LayoutParams lp = new LayoutParams(0, ViewGroup.LayoutParams.FILL_PARENT, 1.0f);
			lp.setMargins(0, 0, 0, 0);
			child.setLayoutParams(lp);
		}

		child.setFocusable(true);
		child.setClickable(true);

		super.addView(child);

		child.setOnClickListener(new TabClickListener(getTabCount() - 1));
		child.setOnFocusChangeListener(this);
	}

	@Override
	public void childDrawableStateChanged(View child)
	{
		if (child == getChildTabViewAt(selectedTab))
		{
			invalidate();
		}
		super.childDrawableStateChanged(child);
	}

	// registered with each tab indicator so we can notify tab host
	private class TabClickListener implements OnClickListener
	{

		private final int tabIndex;

		private TabClickListener(int tabIndex)
		{
			this.tabIndex = tabIndex;
		}

		public void onClick(View v)
		{
			selectionChangedListener.onTabSelectionChanged(tabIndex, true);
		}
	}

	static interface OnTabSelectionChanged
	{
		void onTabSelectionChanged(int tabIndex, boolean clicked);
	}

	public void onFocusChange(View v, boolean hasFocus)
	{
		if (v == this && hasFocus)
		{
			getChildTabViewAt(selectedTab).requestFocus();
			return;
		}

		if (hasFocus)
		{
			int i = 0;
			int numTabs = getTabCount();
			while (i < numTabs)
			{
				if (getChildTabViewAt(i) == v)
				{
					setCurrentTab(i);
					selectionChangedListener.onTabSelectionChanged(i, false);
					break;
				}
				i++;
			}
		}
	}

}
